<template>
  <van-button
    :loading="loading"
    @click="changeFollowed"
    v-if="value"
    round
    size="small"
  >已关注</van-button>
  <van-button
    :loading="loading"
    @click="changeFollowed"
    v-else
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
  >关注</van-button>
</template>
<script>
import { getFollowed, deleteFollowed } from '@/api/user.js'
export default {
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    aut_id: {
      type: [Number, String],
      require: true
    }
  },
  methods: {
    async changeFollowed () {
      this.loading = true
      try {
        //  已关注，取消关注操作
        if (this.value === true) {
          await deleteFollowed(this.aut_id)
        } else {
          await getFollowed(this.aut_id)
        }
        this.$emit('input', !this.value)
      } catch (err) {
        let msg = '操作失败，请稍后重试'
        if (err.response && err.response.status === 400) {
          msg = '你不能关注你自己'
        }
        this.$toast(msg)
      }
      this.loading = false
    }
  }
}
</script>
